package com.xiaoai.httpClientTestService.httpclient.myown;

import com.alibaba.fastjson.JSONObject;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.Test;

import java.io.IOException;

public class HttpclientPostParam {
    
    public static void main(String[] args) throws IOException, ParseException {

        //获取一个客户端
        CloseableHttpClient aDefault = HttpClients.createDefault();
        //创建post请求
        HttpPost httpPost = new HttpPost("http://localhost:8080/test/getp/pp");

        //创建传递参数
        JSONObject param = new JSONObject();
        param.put("name","xiaoai");
        param.put("sex","nan");
        StringEntity stringEntity = new StringEntity(param.toString());

        //请求头设置传参格式
        httpPost.setHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
        httpPost.setEntity(stringEntity);

        //发送请求
        CloseableHttpResponse execute = aDefault.execute(httpPost);

        //
        EntityUtils.toString(execute.getEntity());



    }

    @Test
    public void test(){
//        JSONObject param = new JSONObject();
//        param.put("name","xiaoai");
//        param.put("sex","nan");
//        System.out.println(param.toString());
//        System.out.println(param.toJSONString());

        JSONObject param = new JSONObject();
        param.put("name","xiaoai");
        param.put("sex","nan");

        StringEntity stringEntity = new StringEntity(param.toString());
        System.out.println(stringEntity);


    }

    
    
}
